package com.logicalthinking.socialmall.service.impl;

import com.github.chenlijia1111.utils.common.Result;
import com.github.chenlijia1111.utils.core.PropertyCheckUtil;
import com.logicalthinking.socialmall.common.vo.requestVo.clientUser.ClientUserShopApplyQueryParams;
import com.logicalthinking.socialmall.common.vo.responseVo.clientUser.ShopApplyUserListVo;
import com.logicalthinking.socialmall.dao.ClientAuthenticateShopMapper;
import com.logicalthinking.socialmall.entity.ClientAuthenticateShop;
import com.logicalthinking.socialmall.service.ClientAuthenticateShopServiceI;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 商家认证信息
 *
 * @author chenLiJia
 * @since 2020-02-18 13:56:25
 **/
@Service
public class ClientAuthenticateShopServiceImpl implements ClientAuthenticateShopServiceI {


    @Resource
    private ClientAuthenticateShopMapper clientAuthenticateShopMapper;


    /**
     * 添加
     * 判断如果已经存在就做修改操作
     *
     * @param params 添加参数
     * @return com.github.chenlijia1111.utils.common.Result
     * @author chenLiJia
     * @since 2020-02-18 13:56:25
     **/
    @Override
    public Result add(ClientAuthenticateShop params) {

        ClientAuthenticateShop clientAuthenticateShop = clientAuthenticateShopMapper.selectByPrimaryKey(params.getUserId());
        if (Objects.nonNull(clientAuthenticateShop)) {
            //已经存在,做修改操作
            BeanUtils.copyProperties(params, clientAuthenticateShop);
            //重置修改时间 创建时间置为null
            clientAuthenticateShop.setUpdateTime(new Date());
            clientAuthenticateShop.setCreateTime(null);
            return update(clientAuthenticateShop);
        }

        params.setCreateTime(new Date());
        int i = clientAuthenticateShopMapper.insertSelective(params);
        return i > 0 ? Result.success("操作成功") : Result.failure("操作失败");
    }

    /**
     * 编辑
     *
     * @param params 编辑参数
     * @return com.github.chenlijia1111.utils.common.Result
     * @author chenLiJia
     * @since 2020-02-18 13:56:25
     **/
    @Override
    public Result update(ClientAuthenticateShop params) {

        int i = clientAuthenticateShopMapper.updateByPrimaryKeySelective(params);
        return i > 0 ? Result.success("操作成功") : Result.failure("操作失败");
    }

    /**
     * 根据用户id查询认证信息
     *
     * @param userId
     * @return
     */
    @Override
    public ClientAuthenticateShop findByUserId(Integer userId) {
        return clientAuthenticateShopMapper.selectByPrimaryKey(userId);
    }

    /**
     * 查询申请商家列表
     * 查询 {@link ClientAuthenticateShop}
     *
     * @param params
     * @return
     */
    @Override
    public List<ShopApplyUserListVo> listShopApplyUserListVo(ClientUserShopApplyQueryParams params) {
        List<ShopApplyUserListVo> list = clientAuthenticateShopMapper.listShopApplyUserListVo(params);
        return list;
    }

    /**
     * 条件查询
     *
     * @param condition
     * @return
     * @author chenLiJia
     * @since 2020-02-18 13:56:25
     **/
    @Override
    public List<ClientAuthenticateShop> listByCondition(ClientAuthenticateShop condition) {

        PropertyCheckUtil.transferObjectNotNull(condition, true);
        return clientAuthenticateShopMapper.select(condition);
    }


}
